<meta charset="utf-8">
(#) Wrong Canvas Size

!!! WARNING: Wrong Canvas Size
   This is a warning.

Id
:   `CanvasSize`
Summary
:   Wrong Canvas Size
Severity
:   Warning
Category
:   Correctness
Platform
:   Android
Vendor
:   Android Open Source Project
Feedback
:   https://issuetracker.google.com/issues/new?component=192708
Since
:   3.2.0 (September 2018)
Affects
:   Kotlin and Java files
Editing
:   This check runs on the fly in the IDE editor
Implementation
:   [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/CanvasSizeDetector.kt)
Tests
:   [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/CanvasSizeDetectorTest.kt)

In a custom view's draw implementation, you should normally call
`getWidth` and `getHeight` on the custom view itself, not on the
`canvas` instance.

Canvas width and height are the width and height of the `Canvas`, which
is not always the same as size of the view.

In the hardware accelerated path the width and height of the canvas
typically always match that of the `View` because every view goes to its
own recorded `DisplayList`. But in software rendering there's just one
canvas that is clipped and transformed as it makes its way through the
`View` tree, and otherwise remains the same `Canvas` object for every
View's draw method.

You should only use Canvas state to adjust how much you draw, such as a
quick-reject for early work avoidance if it's going to be clipped away,
but not what you draw.

!!! Tip
   This lint check has an associated quickfix available in the IDE.

(##) Example

Here is an example of lint warnings produced by this check:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text
src/test/pkg/MyCustomView1.java:27:Warning: Calling Canvas.getWidth() is
usually wrong; you should be calling getWidth() instead [CanvasSize]
    int width4 = canvas.getWidth(); // WARN
                 -----------------
src/test/pkg/MyCustomView1.java:28:Warning: Calling Canvas.getHeight()
is usually wrong; you should be calling getHeight() instead
[CanvasSize]
    int height4 = canvas.getHeight(); // WARN
                  ------------------
src/test/pkg/MyCustomView1.java:34:Warning: Calling Canvas.getWidth() is
usually wrong; you should be calling getWidth() instead [CanvasSize]
    int width4 = canvas.getWidth(); // WARN
                 -----------------
src/test/pkg/MyCustomView1.java:35:Warning: Calling Canvas.getHeight()
is usually wrong; you should be calling getHeight() instead
[CanvasSize]
    int height4 = canvas.getHeight(); // WARN
                  ------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here are the relevant source files:

`src/test/pkg/MyCustomView1.java`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~java linenumbers
package test.pkg;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

@SuppressWarnings({"unused", "ClassNameDiffersFromFileName","MethodMayBeStatic"})
@SuppressLint("ViewConstructor")
public class MyCustomView1 extends View {

    public MyCustomView1(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int width1 = getWidth(); // OK
        View view = this;
        int height1 = getHeight(); // OK
        int width2 = this.getWidth(); // OK
        int height2 = this.getHeight(); // OK
        int width3 = view.getWidth(); // OK
        int height3 = view.getHeight(); // OK
        int width4 = canvas.getWidth(); // WARN
        int height4 = canvas.getHeight(); // WARN
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        int width4 = canvas.getWidth(); // WARN
        int height4 = canvas.getHeight(); // WARN
    }

    public void someOtherMethod(Canvas canvas) {
        int width4 = canvas.getWidth(); // OK
        int height4 = canvas.getHeight(); // OK
    }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

`src/test/pkg/MyCustomView2.kt`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin linenumbers
package test.pkg

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.os.Build
import android.util.AttributeSet
import android.view.View

@SuppressLint("ViewConstructor")
class MyCustomView2(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : View(context, attrs, defStyleAttr, defStyleRes) {

    override fun onDraw(canvas: Canvas) {
        val view = this
        val width3 = view.width // OK
        val height3 = view.height // OK
        val width4 = canvas.width // WARN
        val height4 = canvas.height // WARN
        val width5 = canvas.getWidth() // WARN
        val height5 = canvas.getHeight() // WARN
    }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

`src/test/pkg/MyDrawable.java`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~java linenumbers
package test.pkg;

import android.graphics.Canvas;
import android.graphics.drawable.Drawable;

@SuppressWarnings({"unused", "ClassNameDiffersFromFileName","MethodMayBeStatic", "NullableProblems"})
public abstract class MyDrawable extends Drawable {
    @Override
    public void draw(Canvas canvas) {
        int width1 = getBounds().width(); // OK
        int width2 = canvas.getWidth(); // WARN
        int height2 = canvas.getHeight(); // WARN
    }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

`src/test/pkg/MyDrawableKotlin.kt`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin linenumbers
package test.pkg

import android.graphics.Canvas
import android.graphics.drawable.Drawable

abstract class MyDrawableKotlin : Drawable() {
    override fun draw(canvas: Canvas) {
        val width1 = bounds.width() // OK
        val width2 = canvas.width // WARN
        val height2 = canvas.height // WARN
    }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can also visit the
[source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/CanvasSizeDetectorTest.kt)
for the unit tests for this check to see additional scenarios.

The above example was automatically extracted from the first unit test
found for this lint check, `CanvasSizeDetector.testBasic`.
To report a problem with this extracted sample, visit
https://issuetracker.google.com/issues/new?component=192708.

(##) Suppressing

You can suppress false positives using one of the following mechanisms:

* Using a suppression annotation like this on the enclosing
  element:

  ```kt
  // Kotlin
  @Suppress("CanvasSize")
  fun method() {
     problematicStatement()
  }
  ```

  or

  ```java
  // Java
  @SuppressWarnings("CanvasSize")
  void method() {
     problematicStatement();
  }
  ```

* Using a suppression comment like this on the line above:

  ```kt
  //noinspection CanvasSize
  problematicStatement()
  ```

* Using a special `lint.xml` file in the source tree which turns off
  the check in that folder and any sub folder. A simple file might look
  like this:
  ```xml
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;lint&gt;
      &lt;issue id="CanvasSize" severity="ignore" /&gt;
  &lt;/lint&gt;
  ```
  Instead of `ignore` you can also change the severity here, for
  example from `error` to `warning`. You can find additional
  documentation on how to filter issues by path, regular expression and
  so on
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html).

* In Gradle projects, using the DSL syntax to configure lint. For
  example, you can use something like
  ```gradle
  lintOptions {
      disable 'CanvasSize'
  }
  ```
  In Android projects this should be nested inside an `android { }`
  block.

* For manual invocations of `lint`, using the `--ignore` flag:
  ```
  $ lint --ignore CanvasSize ...`
  ```

* Last, but not least, using baselines, as discussed
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).

<!-- Markdeep: --><style class="fallback">body{visibility:hidden;white-space:pre;font-family:monospace}</style><script src="markdeep.min.js" charset="utf-8"></script><script src="https://morgan3d.github.io/markdeep/latest/markdeep.min.js" charset="utf-8"></script><script>window.alreadyProcessedMarkdeep||(document.body.style.visibility="visible")</script>